{extend name="public/help" /}
{block name="oneMenuName"}{$crumbs['oneMenuName']}{/block}
{block name="twoMenuName"}{$crumbs['twoMenuName']}{/block}
{block name="body"}
<div class="main-content">
    <form enctype="multipart/form-data" method="post" id="topform">
        <input type="hidden" value="{$id}" name="id">
        <div class="row">
            <div class="col-lg-6">
                <div class="card alert">
                    <div class="card-header">
                        <h4 >商品添加/修改</h4>
                    </div>
                    <button type="button" id="add_specs" class="btn btn-success m-b-10 m-l-5">添加规格</button>
                    <div class="card-body">
                        <div class="basic-form">
                            <div class="form-group">
                                <label>商家选择</label>
                                <select class="form-control" name="b_id">
                                    {volist name="business" id="vo"}
                                    <option value="{$vo.id}"
                                            {eq name="$vo.id" value="$data['b_id']"}selected{/eq}
                                    >{$vo.name}</option>
                                    {/volist}
                                </select>
                            </div>
                            <div class="form-group">
                                <label>是否团购</label>
                                <select class="form-control" name="is_group_buy" id="is_group_buy">
                                    <option value="1" {eq name="$data.is_group_buy" value="1"}selected{/eq} >-- 单品 --</option>
                                    <option value="2" {eq name="$data.is_group_buy" value="2"}selected{/eq} >-- 团购 --</option>
                                    <!--<option value="3" {eq name="$data.is_group_buy" value="3"}selected{/eq} >&#45;&#45; 全部 &#45;&#45;</option>-->
                                </select>
                            </div>
                            <div class="form-group">
                                <label>商品名称</label>
                                <input type="text" name="goods_name" id="goods_name" class="form-control" value="{$data['goods_name']}" placeholder="商品名称">
                            </div>
                            <div class="form-group">
                                <label>商品分类</label>
                                <select class="form-control" name="type_id" id="type_id">
                                    {volist name="types" id="vo"}
                                    <option value="{$vo.id}"
                                            {eq name="$vo.id" value="$data['type_id']"}selected{/eq}
                                    >{$vo.name}</option>
                                    {/volist}
                                </select>
                            </div>
                            <div class="form-group">
                                <label>商品封面图</label>
                                <input type="text" name="img" id="img" class="form-control" value="{$data['img']}" placeholder="url (七牛云的外链)">
                            </div>
                            <div class="form-group">
                                <label>商品小图</label>
                                <input type="text" name="imgs" id="imgs" class="form-control" value="{$data['imgs']}" placeholder="url (七牛云的外链), 多个用逗号隔开">
                            </div>

                            <div class='card-body' >
                                <table class='table table-responsive'>
                                    <thead>
                                    <tr>
                                        <th></th>
                                        <th>规格名称</th>
                                        <th>非团购价格</th>
                                        <th>库存</th>
                                        <th>团价数量及价格</th>
                                    </tr>
                                    </thead>
                                    <tbody id="down_add_specs">
                                        {if $specs}
                                            {foreach $specs as $k => $v}
                                                <tr>
                                                    <td><button type='button' onclick='del_specs(this)'>删除</button></td>
                                                    <td>{$v.specs_name}</td>
                                                    <td>{$v.price}</td>
                                                    <td>{$v.stock}</td>
                                                    <td>{$v.t_price_number}</td>
                                                    <input type='hidden'  name='specs_name[]' value={$v.specs_name}>
                                                    <input type='hidden' name='stock[]' value={$v.stock}>
                                                    <input type='hidden' name='price[]' value={$v.price}>
                                                    <input type='hidden' name='t_price_number[]' value={$v.t_price_number}>
                                                </tr>
                                            {/foreach}
                                        {/if}
                                    </tbody>
                                </table>
                            </div>

                            <div class="form-group">
                                <label>商品详情</label>
                                <div id="editor" >{$data['desc']}</div>
                                <textarea id="desc" name="desc" style="display: none;"></textarea>
                            </div>

                            <!--<div style="display: none;" id="hidden_input"></div>-->

                            <button type="button" id="submit" class="btn btn-success m-b-10 m-l-5">确定</button>
                        </div>
                    </div>
                </div>
            </div><!-- /# column -->
        </div><!-- /# row -->
    </form>
</div><!-- /# container-fluid -->

{/block}
{block name="js"}
<script src="__ADMIN_JS__/lib/wangEditor-3.1.1/release/wangEditor.js"></script>
<script type="text/javascript">
    var E = window.wangEditor
    var editor = new E('#editor')
    var $text1 = $('#desc')
    editor.customConfig.uploadImgShowBase64 = true

    editor.customConfig.onchange = function (html) {
        // 监控变化，同步更新到 textarea
        $text1.val(html)
    }
    editor.create()
    // 初始化 textarea 的值
    $text1.val(editor.txt.html())
</script>

<script>

    function del_specs(obj){
        $(obj).parent('td').parent('tr').remove()
    }

    $('#add_specs').click(function () {
        layer.open({
            type: 2,
            area: ['600px', '600px'],
            title: '添加商品规格',
            content: '/admin/addGoodsSpecs',
        });
    })

    function add_specs(specs, price, stock, t_price_number){
        var html = '';
        html +=            "<tr>";
        html +=               "<td><button type='button' onclick='del_specs(this)'>删除</button></td>";
        html +=               "<td>"+specs+"</td>";
        html +=                "<td>"+price+"</td>";
        html +=                "<td>"+stock+"</td>";
        html +=                "<td>"+t_price_number+"</td>";
        html +=                "<input type='hidden'  name='specs_name[]' value="+specs+">"
        html +=                "<input type='hidden' name='stock[]' value="+stock+">"
        html +=                "<input type='hidden' name='price[]' value="+price+">"
        html +=                "<input type='hidden' name='t_price_number[]' value="+t_price_number+">"
        html +=            "</tr>";

        // var input = '';
        // input += "<input type='text' name='specs_name[]' value="+specs+">"
        // input += "<input type='text' name='stock[]' value="+stock+">"
        // input += "<input type='text' name='price[]' value="+price+">"
        // input += "<input type='text' name='t_price_number[]' value="+t_price_number+">"


        $('#down_add_specs').append(html)
        // $('#hidden_input').append(input)
    }
</script>

<script>
    $('#submit').click(function(){
        var goods_name = $('#goods_name').val();
        var img = $('#img').val();
        // var price = $('#price').val();
        // var t_price = $('#t_price').val();
        // var stock = $('#stock').val();
        var down_add_specs = $('#down_add_specs').html()
        var desc = $('#desc').val();

        if(!goods_name){ error('商品名称有误'); return; }
        if(!img){ error('商品封面图有误'); return; }
        // if(!price){ error('商品价格有误'); return; }
        // if(!t_price){ error('团购价格有误'); return; }
        // if(!stock){ error('库存有误'); return; }
        if(!down_add_specs){ error('请添加商品规格'); return; }
        if(!desc){ error('商品描述有误'); return; }

        var formData = new FormData($( "#topform" )[0]);

        var id = $('input[name="id"]').val();
        if(id){
            var url = "{:url('/admin/editSaasGoods')}";
        }else{
            var url = "{:url('/admin/addSaasGoods')}";
            formData.delete("id");
        }

        $.ajax({
            type:'post',
            url:url,
            data:formData,
            cache: false,
            contentType: false,
            processData: false,
            success:function(res){
                if(res.code){
                    redirect_success(res.msg, res.url);
                }else{
                    error(res.msg);
                }
            }
        });
    });
</script>
{/block}
